import axios from 'axios'
import {
  likeygoodApi,
  likeygoodListApi,
  deletelikeygoodApi
} from "../../api/likeygood";
export default {
  namespaced: true,
  state: {
    // 点赞的列表数据
    likeygoodlist: [],


  },
  mutations: {
    // 设置点赞的对象数组
    setlikeygoodList(state, list) {
      state.likeygoodlist = list;
      // 重新渲染dom结构
      // this.dispatch("spot/requestALLData",this.state.spot.activeIndex)
    },

    setlikeygoodListAdd(state, addlist) {
      // console.log(this.state.spot.activeIndex);
      let addIndex;

        if (this.state.spot.activeIndex == 1) {
          // console.log(this.state.spot.hotelList);
           this.state.spot.hotelList.filter(
            (item, index) => {
              // console.log(addlist);
              // console.log(Object.values(item));
              // console.log(Object.values(item).indexOf(Object.values(addlist)[0])>-1);
              if (Object.values(item).indexOf(Object.values(addlist)[0]) > -1) {
                // console.log(index);
                return addIndex = index
              }
    
            }
          );
        this.commit("spot/setlikeygoodNumadd",addIndex)
        }else if(this.state.spot.activeIndex == 2){
           this.state.spot.sightList.filter(
            (item, index) => {
              // console.log(addlist);
              // console.log(Object.values(item));
              // console.log(Object.values(item).indexOf(Object.values(addlist)[0])>-1);
              if (Object.values(item).indexOf(Object.values(addlist)[0]) > -1) {
                // console.log(index);
                return addIndex = index
              }
    
            }
          );
        this.commit("spot/setlikeygoodNumadd",addIndex)

        }else if(this.state.spot.activeIndex == 3){
          this.state.spot.foodList.filter(
            (item, index) => {
              // console.log(addlist);
              // console.log(Object.values(item));
              // console.log(Object.values(item).indexOf(Object.values(addlist)[0])>-1);
              if (Object.values(item).indexOf(Object.values(addlist)[0]) > -1) {
                // console.log(index);
                return addIndex = index
              }
    
            }
          );
        this.commit("spot/setlikeygoodNumadd",addIndex)

        }else if(this.state.spot.activeIndex == 4){
          this.state.spot.haiList.filter(
            (item, index) => {
              // console.log(addlist);
              // console.log(Object.values(item));
              // console.log(Object.values(item).indexOf(Object.values(addlist)[0])>-1);
              if (Object.values(item).indexOf(Object.values(addlist)[0]) > -1) {
                // console.log(index);
                return addIndex = index
              }
    
            }
          );
          // console.log(addIndex);
        this.commit("spot/setlikeygoodNumadd",addIndex)

        }
        // console.log(this.state.spot.activeIndex);
      
      // console.log(this.state.spot.sightlList);
      // console.log(this.state.spot[formlist]);
      // this.state.spot[formlist].filter(
      //   (item, index) => {
      //     // console.log(addlist);
      //     // console.log(Object.values(item));
      //     console.log(Object.values(item).indexOf(Object.values(addlist)[0])>-1);
      //     if (Object.values(item).indexOf(Object.values(addlist)[0]) > -1) {
      //       // console.log(index);
      //       return addIndex = index
      //     }

      //   }
      // );
      // console.log(addIndex);
      // console.log("addindex",this.state.spot.hotelList[addIndex].likeygoodNum);

      // this.state.spot.hotelList[addIndex].likeygoodNum++;
      // await this.commit("spot/setlikeygoodNumadd",addIndex)
      this.dispatch("likeygood/requestData")
    },

    // 减少点赞数
    setlikeygoodListDel(state, addlist) {
      // console.log(this.state.spot.hotelList);
      let delIndex;
      if (this.state.spot.activeIndex == 1) {
        // console.log(this.state.spot.hotelList);
         this.state.spot.hotelList.filter(
          (item, index) => {
            // console.log(addlist);
            // console.log(Object.values(item));
            if (Object.values(item).indexOf(Object.values(addlist)[0]) > -1) {
              // console.log(index);
              return delIndex = index
            }
  
          }
        );
      this.commit("spot/setlikeygoodNumdel",delIndex)
      }else if(this.state.spot.activeIndex == 2){
         this.state.spot.sightList.filter(
          (item, index) => {
            // console.log(addlist);
            // console.log(Object.values(item));
            // console.log(Object.values(item).indexOf(Object.values(addlist)[0])>-1);
            if (Object.values(item).indexOf(Object.values(addlist)[0]) > -1) {
              // console.log(index);
              return delIndex = index
            }
  
          }
        );
      this.commit("spot/setlikeygoodNumdel",delIndex)

      }else if(this.state.spot.activeIndex == 3){
        this.state.spot.foodList.filter(
          (item, index) => {
            // console.log(addlist);
            // console.log(Object.values(item));
            // console.log(Object.values(item).indexOf(Object.values(addlist)[0])>-1);
            if (Object.values(item).indexOf(Object.values(addlist)[0]) > -1) {
              // console.log(index);
              return delIndex = index
            }
  
          }
        );
      this.commit("spot/setlikeygoodNumdel",delIndex)

      }else if(this.state.spot.activeIndex == 4){
        this.state.spot.haiList.filter(
          (item, index) => {
            // console.log(addlist);
            // console.log(Object.values(item));
            // console.log(Object.values(item).indexOf(Object.values(addlist)[0])>-1);
            if (Object.values(item).indexOf(Object.values(addlist)[0]) > -1) {
              // console.log(index);
              return delIndex = index
            }
  
          }
        );
        // console.log(addIndex);
      this.commit("spot/setlikeygoodNumdel",delIndex)

      }

      // this.state.spot.hotelList.filter(
      //   (item, index) => {
      //     // console.log(Object.values(item));
      //     // console.log(Object.values(item).indexOf(Object.values(addlist)[0]));
      //     if (Object.values(item).indexOf(Object.values(addlist)[0]) > -1) {
      //       return delIndex = index
      //     }

      //   }
      // );
      // console.log(delIndex);
      // console.log("zheli++++++++++++++++",this.state.spot.hotelList[delIndex].likeygoodNum);
      // this.state.spot.hotelList[delIndex].likeygoodNum--;
      // this.commit("spot/setlikeygoodNumdel",delIndex)
      this.dispatch("likeygood/requestData")
    }
  },

  actions: {

    //请求点赞列表数据
    async requestData(context) {
      // 发送请求
      const {
        data
      } = await likeygoodListApi();
      //  console.log(data);

      // 获得数据
      context.commit("setlikeygoodList", data.list)


    },


    // 添加点赞数据
    async addLikeyGood(context, addlist) {
      // 发送请求
      const {
        data,
        error
      } = await likeygoodApi(addlist);

      if (error) {
        console.error(error);
      }
      // 获得数据
      // console.log(data);

      await context.commit("setlikeygoodListAdd", addlist)

    },



    //删除点赞数据
    async deleteLikeyGood(context, addlist) {
      // 发送请求
      const {
        data,
        error
      } = await deletelikeygoodApi(addlist);

      if (error) {
        console.error(error);
      }

      // 获得数据
      // console.log(data);

      // context.dispatch("requestData")  
      await context.commit("setlikeygoodListDel", addlist)


    }
  },


};